<?php

namespace App\Admin\Override\Field;

use App\Models\Attachements;
use Dcat\Admin\Form\Field\Image;
use Dcat\Admin\Support\Helper;

class ImageView extends Image
{
    protected function initialPreviewConfig(): array
    {
        $previews = [];
        foreach (Helper::array($this->value()) as $path){
            $fileName[] = pathinfo($path)['filename'];
        }
        $attach = Attachements::query()->whereIn('md5_name', $fileName ?? [])->get()->toArray();
        foreach ($attach as $value) {
            $previews[] = [
                'id'   => $value['path'],
                'path' => Helper::basename($value['real_name']),
                'url'  => $this->objectUrl($value['path']),
            ];
        }

        return $previews;
    }
}
